package com.zhou.core.logana;

import com.zhou.config.ToolsConfig;
import com.zhou.core.redis.RedisConst;
import com.zhou.core.logana.handle.ILogAnaHandler;
import com.zhou.core.logana.repository.ILogAnaRepository;
import com.zhou.util.MD5FileUtil;
import com.zhou.util.PropertyFileReader;
import com.zhou.util.SecurityUtils;
import com.zhou.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Created by zhouyongjun on 2020/6/12
 */
public class LogAnaConfig {
    boolean isSaveSwitchOpen=true;
    boolean isReadSwitchOpen=true;
    boolean isZip = false;
    ILogAnaRepository.LogAnaRepositoryType saveRepositoryType = null;
    ILogAnaRepository.LogAnaRepositoryType readRepositoryType = null;
    ILogAnaHandler.LogAnaRecordType recordType = ILogAnaHandler.LogAnaRecordType.JSON;
    int tickPeriodSeconds = 60;
    final static Logger logger = LoggerFactory.getLogger(LogAnaConfig.class);
    String extraRepositoryKey ="";
    PropertyFileReader respoitoryPropertyFileReader;
    List<String> handlerClasses = new ArrayList<>();
    PropertyFileReader serverConfigPropertyFileReader;
    public  void load() throws Throwable
    {
        loadProperties();
    }

    public  void loadProperties()throws Throwable{
        PropertyFileReader reader = new PropertyFileReader();
        Properties temp = new Properties();
//        String fileName = this.getClass().getClassLoader().getResource(ToolsConfig.SCRIPT_FILE_NAME).getPath();
        try
        {
            reader.load(ToolsConfig.LOGANA_FILE_NAME);
            isSaveSwitchOpen = reader.getBooolean("isSaveSwitchOpen",true);
            isReadSwitchOpen = reader.getBooolean("isReadSwitchOpen",true);
            isZip = reader.getBooolean("isZip");
            tickPeriodSeconds  = reader.getInt("tickPeriodSeconds",60);
            _loadRepository(reader);
            _loadRecord(reader);
            loadServerPropertyFileReader(reader);
            logger.info("LogAnaConfig Properties loaded!");
        }
        catch (Exception e)
        {
            logger.error("", e);
        }
    }

      private void loadServerPropertyFileReader(PropertyFileReader reader) throws Exception{
          serverConfigPropertyFileReader = new PropertyFileReader();
          serverConfigPropertyFileReader.put(LoganaConst.SERVER_REGIST_URL,reader.getString(LoganaConst.SERVER_REGIST_URL,""));
          String user = reader.getString(LoganaConst.SERVER_SSH_USER,"");
          String pwd = reader.getString(LoganaConst.SERVER_SSH_PWD,"");
          serverConfigPropertyFileReader.put(LoganaConst.SERVER_SSH_USER,user);
          serverConfigPropertyFileReader.put(LoganaConst.SERVER_SSH_PWD,pwd);
    }
    private void _loadRecord(PropertyFileReader reader) {

        String stringRecordType = reader.getString("record.type");
        if (StringUtils.isNotBlank(stringRecordType ))
        {
            recordType = ILogAnaHandler.LogAnaRecordType.valueOf(stringRecordType.toUpperCase());
        }
        _loadHandlerClasses(reader);
    }

    private void _loadHandlerClasses(PropertyFileReader reader) {
        List<String> handlerClasses = new ArrayList<>();
        String classes = reader.getString("record.handler.classes");
        if (StringUtils.isEmpty(classes)) return;
        for (String clazz : classes.split(","))
        {
            if (StringUtils.isEmpty(clazz)) continue;
            handlerClasses.add(clazz);

        }
        this.handlerClasses = handlerClasses;
    }

    private void _loadRepository(PropertyFileReader reader) throws Exception{
        String stringSaveRepositoryType = reader.getString("repository.save.type");
        if (StringUtils.isNotBlank(stringSaveRepositoryType ))
        {
            saveRepositoryType = ILogAnaRepository.LogAnaRepositoryType.valueOf(stringSaveRepositoryType.toUpperCase());
        }
        String stringReadRepositoryType = reader.getString("repository.read.type");

        if (StringUtils.isNotBlank(stringReadRepositoryType ))
        {
            readRepositoryType = ILogAnaRepository.LogAnaRepositoryType.valueOf(stringReadRepositoryType.toUpperCase());
        }
        if (saveRepositoryType == ILogAnaRepository.LogAnaRepositoryType.REDIS)
        {
            loadRedisPropertyFileReader(reader);
        }
        extraRepositoryKey = reader.getString("extraRepositoryKey","");
    }

    private void loadRedisPropertyFileReader(PropertyFileReader reader) throws Exception{
        respoitoryPropertyFileReader = new PropertyFileReader();
        respoitoryPropertyFileReader.put(RedisConst.CONFIG_KEY_IP,reader.getString(RedisConst.CONFIG_KEY_IP,"localhost"));
        respoitoryPropertyFileReader.put(RedisConst.CONFIG_KEY_PORT,reader.getInt(RedisConst.CONFIG_KEY_PORT,6379));
        respoitoryPropertyFileReader.put(RedisConst.CONFIG_KEY_USE_INDEX,reader.getInt(RedisConst.CONFIG_KEY_USE_INDEX,0));
        respoitoryPropertyFileReader.put(RedisConst.CONFIG_KEY_MAX_ACTIVE,reader.getInt(RedisConst.CONFIG_KEY_MAX_ACTIVE,20));
        respoitoryPropertyFileReader.put(RedisConst.CONFIG_KEY_MAX_IDLE,reader.getInt(RedisConst.CONFIG_KEY_MAX_IDLE,10));
        respoitoryPropertyFileReader.put(RedisConst.CONFIG_KEY_MAX_WAIT,reader.getInt(RedisConst.CONFIG_KEY_MAX_WAIT,1000));
        respoitoryPropertyFileReader.put(RedisConst.CONFIG_KEY_TEST_ON_BORROW,reader.getBooolean(RedisConst.CONFIG_KEY_TEST_ON_BORROW,true));
        respoitoryPropertyFileReader.put(RedisConst.CONFIG_KEY_TEST_ON_RETURN,reader.getBooolean(RedisConst.CONFIG_KEY_TEST_ON_RETURN,true));
        respoitoryPropertyFileReader.put(RedisConst.CONFIG_KEY_TIMEOUT,reader.getInt("reids.timeOut",1000));
    }

    public boolean isZip() {
        return isZip;
    }

    public void setZip(boolean zip) {
        isZip = zip;
    }

    public ILogAnaRepository.LogAnaRepositoryType getSaveRepositoryType() {
        return saveRepositoryType;
    }

    public void setSaveRepositoryType(ILogAnaRepository.LogAnaRepositoryType saveRepositoryType) {
        this.saveRepositoryType = saveRepositoryType;
    }

    public ILogAnaRepository.LogAnaRepositoryType getReadRepositoryType() {
        return readRepositoryType;
    }

    public void setReadRepositoryType(ILogAnaRepository.LogAnaRepositoryType readRepositoryType) {
        this.readRepositoryType = readRepositoryType;
    }

    public ILogAnaHandler.LogAnaRecordType getRecordType() {
        return recordType;
    }

    public void setRecordType(ILogAnaHandler.LogAnaRecordType recordType) {
        this.recordType = recordType;
    }

    public int getTickPeriodSeconds() {
        return tickPeriodSeconds;
    }

    public void setTickPeriodSeconds(int tickPeriodSeconds) {
        this.tickPeriodSeconds = tickPeriodSeconds;
    }

    public void setRespoitoryPropertyFileReader(PropertyFileReader respoitoryPropertyFileReader) {
        this.respoitoryPropertyFileReader = respoitoryPropertyFileReader;
    }

    public String getExtraRepositoryKey() {
        return extraRepositoryKey;
    }

    public boolean isSaveSwitchOpen() {
        return isSaveSwitchOpen;
    }

    public void setSaveSwitchOpen(boolean saveSwitchOpen) {
        isSaveSwitchOpen = saveSwitchOpen;
    }

    public boolean isReadSwitchOpen() {
        return isReadSwitchOpen;
    }

    public PropertyFileReader getRespoitoryPropertyFileReader() {
        return respoitoryPropertyFileReader;
    }

    public void setReadSwitchOpen(boolean readSwitchOpen) {
        isReadSwitchOpen = readSwitchOpen;
    }

    public void setExtraRepositoryKey(String extraRepositoryKey) {
        this.extraRepositoryKey = extraRepositoryKey;
    }

    public List<String> getHandlerClasses() {
        return handlerClasses;
    }

    public void setHandlerClasses(List<String> handlerClasses) {
        this.handlerClasses = handlerClasses;
    }

    public PropertyFileReader getServerConfigPropertyFileReader() {
        return serverConfigPropertyFileReader;
    }

    public void setServerConfigPropertyFileReader(PropertyFileReader serverConfigPropertyFileReader) {
        this.serverConfigPropertyFileReader = serverConfigPropertyFileReader;
    }
}
